﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace CommonLib
{

    /// <summary>
    /// 加解密
    /// </summary>
    public class EncryptionLib
    {

        /// <summary>
        /// 取得經過 MD5 加密後的字串
        /// </summary>
        /// <param name="source">欲加密的字串</param>
        /// <param name="isNull">若是加密失敗時，將回傳 null 或是拋出 Exception(True: null ; False : Exception)</param>
        /// <returns></returns>
        public static string GetEncryptor(string source, bool isNull)
        {
            string eKey = EssentialConfig.encryptKey; // System.Web.Configuration.WebConfigurationManager.AppSettings["encryptKey"];
            string encrStr = "";
            string errMsg = "";

            try
            {
                TripleDESCryptoServiceProvider MyDes = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider MyMd5 = new MD5CryptoServiceProvider();
                MyDes.Key = MyMd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(eKey));
                MyDes.Mode = CipherMode.ECB;
                ICryptoTransform DesEncrypt = MyDes.CreateEncryptor();

                byte[] buffer = System.Text.Encoding.GetEncoding("BIG5").GetBytes(source);
                encrStr = Convert.ToBase64String(DesEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch (Exception ex)
            {
                errMsg = "MD5 加密失敗: " + ex.Message;
            }

            if (errMsg == "")
                return encrStr;
            else if (isNull)
                return null;
            else
                throw new ArgumentException(errMsg);
        }

        /// <summary>
        /// 取得經過 MD5 解密後的字串
        /// </summary>
        /// <param name="source">欲解密的字串</param>
        /// <param name="isNull">若是解密失敗時，將回傳 null 或是拋出 Exception(True: null ; False : Exception)</param>
        /// <returns></returns>
        public static string GetDecryptor(string source, bool isNull)
        {
            string eKey = EssentialConfig.encryptKey;  // System.Web.Configuration.WebConfigurationManager.AppSettings["encryptKey"];
            string decrStr = "";
            string errMsg = "";

            try
            {
                TripleDESCryptoServiceProvider MyDes = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider MyMd5 = new MD5CryptoServiceProvider();
                MyDes.Key = MyMd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(eKey));
                MyDes.Mode = CipherMode.ECB;
                ICryptoTransform DesEncrypt = MyDes.CreateDecryptor();

                byte[] buffer = Convert.FromBase64String(source);
                decrStr = System.Text.Encoding.GetEncoding("BIG5").GetString(DesEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
            }
            catch (Exception ex)
            {
                errMsg = "MD5 解密失敗: " + ex.Message;
            }

            if (errMsg == "")
                return decrStr;
            else if (isNull)
                return null;
            else
                throw new ArgumentException(errMsg);
        }


        public static string GetEncryFKey(DateTime dt, string fKey)
        {
            string eFKey = dt.ToString("yyyy/MM/dd HH:mm:ss") + fKey;
            eFKey = GetEncryptor(eFKey, true);

            return eFKey;
        }

        public static string GetDecryFKey(string vfKey)
        {
            string rtStr;
            try
            {
                string tFKey = GetDecryptor(vfKey, true);
                rtStr = tFKey.Substring(19);
                return rtStr;
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
